package com.rightsidetech.trade.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.trade.dtos.request.integral.IntegralOperateRecordReq;
import com.rightsidetech.trade.dtos.request.integral.IntegralSaveReq;
import com.rightsidetech.trade.dtos.request.integral.LogisticsOrderNoReq;
import com.rightsidetech.trade.dtos.response.integral.IntegralConvertRecordResp;
import com.rightsidetech.trade.dtos.response.integral.IntegralOperateRecordResp;
import com.rightsidetech.trade.dtos.response.integral.IntegralOrderDetailResp;
import com.rightsidetech.trade.entity.TdIntegralOrder;
import com.rightsidetech.trade.mapper.TdIntegralOrderMapper;
import com.rightsidetech.trade.service.TdIntegralOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

@Service("tdIntegralOrderService")
public class TdIntegralOrderServiceImpl implements TdIntegralOrderService {

    @Resource
    private TdIntegralOrderMapper tdIntegralOrderMapper;

    @Override
    public ResponseData<String> saveOrder(IntegralSaveReq req) {
        TdIntegralOrder record = BeanUtil.copyProperties(req, TdIntegralOrder.class);
        record.setCreatedAt(new Date());
        if (tdIntegralOrderMapper.insertSelective(record) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(record.getOrderNo());
    }

    @Override
    public ResponseData<PageInfo<IntegralConvertRecordResp>> orderPage(PageRequest req, Long storeId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<IntegralConvertRecordResp> result = tdIntegralOrderMapper.selectAppPageList(storeId);
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData<IntegralOrderDetailResp> orderDetail(Long id,String orderNo) {
        IntegralOrderDetailResp result = tdIntegralOrderMapper.selectDetail(id,orderNo);
        if(ObjectUtil.isEmpty(result)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该订单");
        }
        return ResponseData.success(result);
    }

    @Override
    public ResponseData<PageInfo<IntegralOperateRecordResp>> operateRecordPage(IntegralOperateRecordReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<IntegralOperateRecordResp> result = tdIntegralOrderMapper.selectOperateRecordPage(req);
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData logisticsOrderNo(LogisticsOrderNoReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        TdIntegralOrder order = tdIntegralOrderMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(order)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到需要操作的数据");
        }
        order = new TdIntegralOrder();
        order.setId(req.getId());
        order.setLogisticsOrderNo(req.getLogisticsOrderNo());
        if (tdIntegralOrderMapper.updateByPrimaryKeySelective(order) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public List<IntegralOperateRecordResp> operateRecordList(IntegralOperateRecordReq req) {
        return tdIntegralOrderMapper.selectOperateRecordPage(req);
    }

    @Override
    public TdIntegralOrder selectByOrderNo(String orderNo) {
        return tdIntegralOrderMapper.selectOrderByOrderNo(orderNo);
    }

    @Override
    public int updateByPrimaryKeySelective(TdIntegralOrder order){
        return tdIntegralOrderMapper.updateByPrimaryKeySelective(order);
    }
}
